﻿using KAs.Application.Contract.Dto;
using KAs.Common.Const;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Test.Application.Contract.Dto;
using Test.Application.Contract.Svc;

namespace Test.Api;

/// <summary>
/// 用户
/// </summary>
[Route(ConstConfig.API_ROUTE_CONFIG)]
[ApiController]
public class UserController : ControllerBase
{

    private readonly IUserAppService _appService;

    public UserController(IUserAppService appService)
    {
        _appService = appService;
    }

    /// <summary>
    /// 登录
    /// </summary>
    /// <param name="dto">登录dto</param>
    /// <returns>登录信息</returns>
    /// <remarks>
    /// AllowAnonymous, 无需token
    /// </remarks>
    [HttpPost]
    [AllowAnonymous]
    public async Task<ApiResult<LoginReturnDto>> LoginAsync(LoginDto dto)
    {
        var result = ApiResult<LoginReturnDto>.IsSuccess();
        result.Data = await _appService.LoginAsync(dto);
        return result;
    }

    /// <summary>
    /// 注册
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost]
    [AllowAnonymous]
    public async Task<ApiResult<UserDto>> RegisterAsync(RegisterDto dto)
    {
        var result = ApiResult<UserDto>.IsSuccess();
        result.Data = await _appService.RegisterAsync(dto);
        return result;
    }

    /// <summary>
    /// 获取当前用户信息
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public async Task<ApiResult<UserInfoDto>> GetUserInfoAsync()
    {
        var result = ApiResult<UserInfoDto>.IsSuccess();
        result.Data = await _appService.GetUserInfoAsync();
        return result;
    }
}
